package com.logistics.controller;

import com.logistics.common.Result;
import com.logistics.entity.TransportRecord;
import com.logistics.service.TransportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/transport-record")
public class TransportRecordController {
    @Autowired
    private TransportRecordService transportRecordService;

    @PostMapping
    public Result<TransportRecord> create(@RequestBody TransportRecord record) {
        return transportRecordService.createTransport(record);
    }

    @GetMapping("/{id}")
    public Result<TransportRecord> getById(@PathVariable Long id) {
        return transportRecordService.getTransport(id);
    }

    @GetMapping("/no/{transportNo}")
    public Result<TransportRecord> getByTransportNo(@PathVariable String transportNo) {
        return transportRecordService.getTransportByTransportNo(transportNo);
    }

    @GetMapping("/order/{orderId}")
    public Result<List<TransportRecord>> listByOrderId(@PathVariable Long orderId) {
        return transportRecordService.getTransportsByOrderId(orderId);
    }

    @GetMapping("/list")
    public Result<List<Map<String, Object>>> list() {
        return transportRecordService.listTransports();
    }

    @GetMapping("/search")
    public Result<List<Map<String, Object>>> search(
            @RequestParam(required = false) String transportNo,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return transportRecordService.searchTransports(transportNo, status, startDate, endDate);
    }

    @PutMapping
    public Result<TransportRecord> update(@RequestBody TransportRecord record) {
        return transportRecordService.updateTransport(record);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        return transportRecordService.deleteTransport(id);
    }

    @PutMapping("/{id}/status/{status}")
    public Result<TransportRecord> updateStatus(@PathVariable Long id, @PathVariable String status) {
        return transportRecordService.updateTransportStatus(id, status);
    }
} 